package fr.imag.adele.cprobes.impl;

import java.util.StringTokenizer;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;

import fr.imag.adele.cprobes.services.DataSender;
import fr.imag.adele.cprobes.services.Probe;
import fr.imag.adele.cprobes.util.DataSet;

public class StandardDataSender implements DataSender {
	private Probe 			m_probe;						// the parent probe
	private String	 		m_providedResources;			// indicates which resources are provided by the probe		
	
	private BundleContext 	m_bundleContext;				// the Bundle context (OSGi). Used to register a event publisher
	private ServiceTracker 	m_tracker;						// Service Tracker to retrieve the EventAdmin service						
	private EventAdmin 		ea;								// the EventAdmin service used to send the provided events
	
	/**
	 * Constructor
	 * Used to retrieve the BundleContext
	 * @param bc BundleContext
	 */
	public StandardDataSender(BundleContext bc){
		this.m_bundleContext = bc;		
	}
	/**
	 * Called by the probe to send the processed data to interested targets
	 * @param dataSet a list of data events
	 */
	public void sendData(DataSet dataSet) {
		if (dataSet != null) {
			for (int i=0; i<dataSet.size(); i++){				
				if (isProvidedResource(dataSet.getDataEvent(i).getResource())==true){			
					if (ea != null){
						ea.sendEvent(new Event(dataSet.getDataEvent(i).getSource() + "/" + 
								dataSet.getDataEvent(i).getResource(), 
								dataSet.getDataEvent(i).getAllData()));		
						System.out.println("[SEND] " + getProbe().getProbeId() + ": " + dataSet.getDataEvent(i).getSource() + "/" + 
								dataSet.getDataEvent(i).getResource() + ": " + dataSet.getDataEvent(i).getValue());
					}
				}
			}
		}
	}	
	/**
	 * Test whether this event is provided by the actual probe
	 * @return true if it is provided
	 */
	protected boolean isProvidedResource(String resource){
		String[] resources = getProvidedResources(); 
		if (resources != null){
			for (int i=0; i<resources.length; i++){
				if (resources[i].equals(resource))
					return true;		
			}
		}
		return false;
	}	
	/**
	 * Called when the DataCollector instance started
	 */
	public void starting() {
		m_tracker = new ServiceTracker(m_bundleContext, EventAdmin.class.getName(), null);
		m_tracker.open();
		ea = (EventAdmin)m_tracker.getService();		
	}
	/**
	 * Called when the DataCollected instance stopped
	 */
	public void stoping() {
		m_tracker.close();
	}

	//// GETTERS and SETTERS
	
	/**
	 * get the list of the provided resources of the probe
	 * @return list of provided resources
	 */
	public String[] getProvidedResources() {
		StringTokenizer st = new StringTokenizer(m_providedResources," ",false);
		String t="";
		while (st.hasMoreElements()) t += st.nextElement();
		String[] resources = t.split(",");
		return resources;
	}
	public void setProvidedResources(String resources){
		this.m_providedResources = resources;
	}
	public void setProbe(Probe probe) {
		this.m_probe = probe;
	}
	public Probe getProbe() {
		return m_probe;
	}
	
	////
}
